package services

import (
	"errors"
	"hft/Database/admin"
	"hft/global"
)

type CountrySetting struct{}

func (*CountrySetting) Edit(data *admin.CountrySetting) (err error) {

	global.OC_DB.Model(&admin.CountrySetting{}).Where("id = ?", 1).Updates(map[string]interface{}{
		"lpr":        data.Lpr,
		"base_fund":  data.BaseFund,
		"qr_code_wx": data.QrCodeWx,
		"fran_phone": data.FranPhone,
	})

	return nil
}

func (*CountrySetting) Info() (data *admin.CountrySetting, err error) {

	var info *admin.CountrySetting

	global.OC_DB.First(&info)

	return info, nil

}

func (*CountrySetting) EditAssist(data *admin.Assist) (err error) {

	global.OC_DB.Model(&admin.Assist{}).Where("id = ?", 1).Updates(map[string]interface{}{"cont": data.Cont, "title": data.Title})

	return nil

}

func (*CountrySetting) EditPrivate(data *admin.Private) (err error) {

	global.OC_DB.Model(&admin.Private{}).Where("id = ?", 1).Updates(map[string]interface{}{"cont": data.Cont, "title": data.Title})

	return nil

}

func (*CountrySetting) GetAssist() (data *admin.Assist, err error) {

	var assist *admin.Assist

	global.OC_DB.First(&assist)

	return assist, nil

}

func (*CountrySetting) GetPrivate() (data *admin.Private, err error) {

	var assist *admin.Private

	global.OC_DB.First(&assist)

	return assist, nil

}

func (*CountrySetting) CreateOri(data *admin.Origin) (err error) {

	res := global.OC_DB.Where("name = ?", data.Name).FirstOrCreate(&data)

	if res.Error != nil {
		return errors.New("未知错误500")
	}

	if res.RowsAffected == 0 {
		return errors.New("该端口已存在")
	}

	return nil
}

func (*CountrySetting) UpdateOri(data *admin.Origin) (err error) {

	err = global.OC_DB.Model(&admin.Origin{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
		"name":      data.Name,
		"is_as_app": data.IsAsApp,
	}).Error

	if err != nil {
		return errors.New("该端口已存在")
	}
	return nil
}

func (*CountrySetting) DeleteOri(ids []int) (err error) {
	var list []*admin.Origin

	global.OC_DB.Find(&list, ids)

	global.OC_DB.Model(&admin.Origin{}).Unscoped().Delete(&list)

	return nil
}

func (*CountrySetting) GetOriList() (list []*admin.Origin, err error) {

	var newList []*admin.Origin

	global.OC_DB.Find(&newList)

	return newList, nil

}

var CountrySettingService = new(CountrySetting)
